package pers.xds.wtuapp.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import pers.xds.wtuapp.errorcode.ResponseWrapper;
import pers.xds.wtuapp.security.SecurityConstant;
import pers.xds.wtuapp.security.UserPrincipal;
import pers.xds.wtuapp.web.aop.RateLimit;
import pers.xds.wtuapp.web.database.bean.Acquisition;
import pers.xds.wtuapp.web.database.group.InsertGroup;
import pers.xds.wtuapp.web.database.view.AcquisitionQuery;
import pers.xds.wtuapp.web.es.bean.EsAcquisition;
import pers.xds.wtuapp.web.security.util.SecurityContextUtil;
import pers.xds.wtuapp.web.service.AcquisitionService;
import pers.xds.wtuapp.web.util.StringUtils;

import java.util.List;

/**
 * @author DeSen Xu
 * @date 2023-02-16 17:26
 */
@RestController
@RequestMapping("acquisition")
@PreAuthorize(SecurityConstant.EL_AUTHENTICATED)
public class AcquisitionController {

    private AcquisitionService acquisitionService;

    @Autowired
    public void setAcquisitionService(AcquisitionService acquisitionService) {
        this.acquisitionService = acquisitionService;
    }

    private static final int POST_ACQUISITION_MAX_ALLOW = 10;

    @PostMapping("post")
    @RateLimit(value = POST_ACQUISITION_MAX_ALLOW, limitMessage = "您每天最多发布" + POST_ACQUISITION_MAX_ALLOW + "次收购请求")
    public ResponseWrapper<Integer> postAcquisition(@Validated(InsertGroup.class) Acquisition acquisition) {
        UserPrincipal userPrincipal = SecurityContextUtil.getUserPrincipal();
        acquisition.setOwnerId(userPrincipal.getId());
        return ResponseWrapper.success(acquisitionService.postAcquisition(acquisition));
    }


    @GetMapping("query")
    @PreAuthorize(SecurityConstant.EL_PERMIT_ALL)
    public ResponseWrapper<List<AcquisitionQuery>> queryNewlyAcquisition(
            @RequestParam(required = false, value = "m") Integer maxId,
            @RequestParam(required = false, defaultValue = "5", value = "s") int size
    ) {
        return ResponseWrapper.success(acquisitionService.queryList(maxId, size));
    }

    @PostMapping("/op/{id}/take_down")
    public ResponseWrapper<Void> takeDownAcquisition(@PathVariable int id) {
        UserPrincipal userPrincipal = SecurityContextUtil.getUserPrincipal();
        acquisitionService.takeDown(id, userPrincipal.getId());
        return ResponseWrapper.success();
    }

    @GetMapping("/op/{id}")
    @PreAuthorize(SecurityConstant.EL_PERMIT_ALL)
    public ResponseWrapper<Acquisition> queryById(@PathVariable int id) {
        return ResponseWrapper.success(acquisitionService.queryById(id));
    }

    @GetMapping("/uploaded")
    public ResponseWrapper<List<Acquisition>> queryByActive(
            @RequestParam("a") String active,
            @RequestParam(value = "p", defaultValue = "1") int page,
            @RequestParam(value = "s", defaultValue = "5") int size
    ) {
        UserPrincipal userPrincipal = SecurityContextUtil.getUserPrincipal();
        return ResponseWrapper.success(acquisitionService.queryByActive(userPrincipal.getId(), StringUtils.isTrue(active), page, size));
    }

    @GetMapping("/search")
    @PreAuthorize(SecurityConstant.EL_PERMIT_ALL)
    public ResponseWrapper<List<EsAcquisition>> searchByTitle(
            @RequestParam("t") String title,
            @RequestParam(value = "p", defaultValue = "1") int page,
            @RequestParam(value = "s", defaultValue = "5") int size
    ) {
        return ResponseWrapper.success(acquisitionService.searchByTitle(title, page, size));
    }

}
